﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using GUI.Extensions;

namespace GUI.Controls.News
{
    public partial class EmbedNews : System.Web.UI.UserControl
    {
        private const string Format = "<li class=\"daily-item\">{3}<h2><a class=\"daily-title\" href=\"{0}\" title=\"{2}\">{1}</a></h2></li>";
        private const int Width = 315;
        private const int Height = 160;
        private const int Top = 9;
        private static readonly bool EnableCacheHtml =  CacheSettingExtensions.EnableCacheHtml;

        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var dt = bo.GetEmbedNewsOnPage(top: Top, zoneId: 0, type: 1); // type của box chân trang chủ
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {

                var sb = new System.Text.StringBuilder();

                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, Width, Height, lazyLoad: true);

                    sb.AppendFormat(Format,
                        r[SqlConstants.NewsPublish.Url],
                        r[SqlConstants.NewsPublish.Title],
                        PresentationUtils.EnCode(r[SqlConstants.NewsPublish.Title].ToString()),
                        avatar);

                }
                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }
    }
}